#/usr/bin/env bash
set -eu
arch='arm-gem5'
while getopts 'a:' OPT; do
  case "$OPT" in
    a)
      arch="$OPTARG"
      ;;
  esac
done
shift "$(($OPTIND - 1))"
if [ ! -d 'gem5' ]; then
  git clone https://gem5.googlesource.com/public/gem5
fi
cd gem5
git checkout da79d6c6cde0fbe5473ce868c9be4771160a003b
system_dir='../gem5-system'
binaries_dir="${system_dir}/binaries"
disks_dir="${system_dir}/disks"
mkdir -p "$binaries_dir" "$disks_dir"
if [ "$arch" = 'x86_64-gem5' ]; then
  CC=gcc-6 CXX=g++-6 scons -j"$(nproc)" --ignore-style build/X86/gem5.opt
  f="${disks_dir}/linux-bigswap2.img"
  dd if=/dev/zero of="$f" bs=1024 count=65536
  mkswap "$f"
  # This file must always be present, despite --kernel overriding that default and selecting the kernel.
  # I'm not even joking. No one has ever built x86 gem5 without the magic dist dir present.
  touch "${binaries_dir}/x86_64-vmlinux-2.6.22.9"
elif [ "$arch" = 'arm-gem5' ]; then
  # Compilation fails with gcc 7 on that commit.
  # There were some recent portability patches, so it will likely get there soon.
  CC=gcc-6 CXX=g++-6 scons -j"$(nproc)" --ignore-style build/ARM/gem5.opt
  make -C system/arm/dt
  make -C system/arm/simple_bootloader
  make -C system/arm/aarch64_bootloader/
  cp ./system/arm/simple_bootloader/boot_emm.arm "$binaries_dir"
  cp ./system/arm/aarch64_bootloader/boot_emm.arm64 "$binaries_dir"
fi
